package android.slc.extras.component.tree.ui

import android.app.Application
import android.os.Bundle
import android.slc.appbase.vm.AppBaseViewModel
import android.slc.extras.component.tree.config.ConstantsTree
import android.slc.extras.component.tree.entity.SlcTree
import android.slc.extras.component.tree.entity.SlcTreeNav
import androidx.databinding.Observable
import androidx.databinding.ObservableField
import java.io.Serializable

/**
 * 多选vm
 * @Description:
 * @Author: sunlunchang
 * @CreateDate: 2021/12/06 下午 2:42
 *
 */
abstract class TreeListMultipleVm<T : SlcTree, TP : Serializable>(application: Application) :
    AppBaseViewModel(application) {
    private var mTreeId: String = ConstantsTree.VALUE_DEF_TREE_ID

    val dataListVmBox: TreeListMultipleVmBox<T, TP> by lazy {
        createDataListVmBox()
    }

    lateinit var treeMultipleShelfVmBox: TreeMultipleShelfVmBox<T>
    val slcTreeNavOf = ObservableField<SlcTreeNav>()

    open fun initVm(bundle: Bundle, treeMultipleShelfVmBox: TreeMultipleShelfVmBox<T>) {
        registerVmBox(this.dataListVmBox)
        this.treeMultipleShelfVmBox = treeMultipleShelfVmBox
        this.mTreeId =
            bundle.getString(ConstantsTree.KEY_TREE_ID) ?: ConstantsTree.VALUE_DEF_TREE_ID
        this.dataListVmBox.initVmBox(
            this.mTreeId,
            treeMultipleShelfVmBox.getSelectListByTreeId(this.mTreeId),
            treeMultipleShelfVmBox.mSelectNotParentList,
            bundle
        )
        this.dataListVmBox.refresh()
        this.slcTreeNavOf.set(
            SlcTreeNav().apply {
                this.treeName = this@TreeListMultipleVm.dataListVmBox.treeName
            })
        this.dataListVmBox.selectChangeNotify.addOnPropertyChangedCallback(object :
            Observable.OnPropertyChangedCallback() {
            override fun onPropertyChanged(sender: Observable?, propertyId: Int) {
                this@TreeListMultipleVm.treeMultipleShelfVmBox.notifySelectChange()
                /*this@TreeListMultipleVm.treeMultipleShelfVmBox.putSelectDataList(
                    this@TreeListMultipleVm.treeId,
                    this@TreeListMultipleVm.dataListVmBox.getSelectTreeList()
                )*/
            }
        })
    }

    protected abstract fun createDataListVmBox(): TreeListMultipleVmBox<T, TP>

}